package com.aimi.servlet;

import com.aimi.entity.User;
import com.aimi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController        //声明本类是一个web层bean,且返回一个json数据
public class UserController {
    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @GetMapping("/users")
    public List<User> handleShowUsers(){
        System.out.println("通过GET方法获取全部用户信息");
        List<User> userList = userService.queryUsers();
        return userList;
    }

    @GetMapping("/info")
    public String handleInfo(Integer id, HttpSession session){
        System.out.println("编辑编号为：" + id + "的用户的信息");
        session.setAttribute("id", id);
        return "/users/showuser.html";
    }

    @GetMapping("/getinfo")
    public User handleGetInfo(HttpSession session){
        Integer id = (Integer)session.getAttribute("id");
        System.out.println("编号是：" + id);
        User user = userService.queryUser(id);

        return user;
    }

    @PostMapping("/changeinfo")
    public String handleChange(@RequestBody User user){
        userService.updateUser(user);

        return "/users/showusers.html";
    }

    @GetMapping("/deleteinfo")
    public List<User> handleDelete(Integer id){
        System.out.println(id);
        userService.deleteUser(id);
        List<User> userList = userService.queryUsers();
        return userList;
    }
}
